package cn.wolfcode.web.controller;


import cn.wolfcode.transfer.domain.MessageBoard;
import cn.wolfcode.transfer.domain.MessageReply;
import cn.wolfcode.transfer.service.IMessageBoardService;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IMessageReplyService;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private IMessageBoardService messageBoardService;
    @Autowired
    private IMessageReplyService messageReplyService;


    @RequiredPermission(name = "留言表页面", expression = "message:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "message/list";
    }

    @RequiredPermission(name = "留言表详情页面", expression = "message:details")
    @RequestMapping("/details")
    public String details(Model model, Long id) {
        MessageBoard messageBoard = messageBoardService.get(id);
        model.addAttribute("messageBoard",messageBoard);
        List<MessageReply> list = messageReplyService.selectByMessageId(id);
        model.addAttribute("list", list);
        return "message/details";
    }


    @RequiredPermission(name = "留言表删除", expression = "message:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            messageBoardService.delete(id);
        }
        return "redirect:/message/list";
    }


    @RequiredPermission(name = "留言表新增/编辑", expression = "message:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoard messageBoard) {
        if (messageBoard.getId() == null) {
            messageBoardService.save(messageBoard);
        } else {
            messageBoardService.update(messageBoard);
        }
        return "redirect:/message/list";
    }


    @RequiredPermission(name = "留言表新增/编辑", expression = "message:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("messageBoard", messageBoardService.get(id));
        }
        return "message/input";
    }
}
